package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CompanyActivityApplyAddDTO;
import com.oa.admin.dto.CompanyActivityApplyListDTO;
import com.oa.admin.service.ICompanyActivityFileService;
import com.oa.admin.vo.CompanyActivityApplyDetailsVO;
import com.oa.admin.vo.CompanyActivityApplyListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.CompanyActivityApply;
import com.oa.admin.mapper.CompanyActivityApplyMapper;
import com.oa.admin.service.ICompanyActivityApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.service.IApplyApprovalRecordService;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IApprovalNodeService;
import com.oa.system.service.IApprovalProcessDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 公司活动申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class CompanyActivityApplyServiceImpl extends ServiceImpl<CompanyActivityApplyMapper, CompanyActivityApply> implements ICompanyActivityApplyService {

    private final CompanyActivityApplyMapper companyActivityApplyMapper;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IApprovalProcessDefineService approvalProcessDefineService;

    @Autowired
    private IApprovalNodeService approvalNodeService;

    @Autowired
    private IApplyApprovalRecordService applyApprovalRecordService;

    @Autowired
    public CompanyActivityApplyServiceImpl(CompanyActivityApplyMapper companyActivityApplyMapper) {
        this.companyActivityApplyMapper = companyActivityApplyMapper;
    }

    @Autowired
    private ICompanyActivityFileService companyActivityFileService;

    @Override
    @CommonService
    public PageResult<CompanyActivityApply> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<CompanyActivityApply> list = companyActivityApplyMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<CompanyActivityApply> page = new Page<>(pageNum, pageSize);
            //查询
            List<CompanyActivityApply> list = companyActivityApplyMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public CompanyActivityApplyDetailsVO findCompanyActivityApplyById(String id){
        return companyActivityApplyMapper.companyActivityApplyDetails(id);
    }

    @CommonService
    @Override
    public PageResult<CompanyActivityApplyListVO> findCompanyActivityApplyList(CompanyActivityApplyListDTO companyActivityApplyListDTO) {
        return new PageResult<>(companyActivityApplyMapper.findCompanyActivityApplyList(companyActivityApplyListDTO.getPage(), companyActivityApplyListDTO));
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(CompanyActivityApplyAddDTO companyActivityApplyAddDTO) throws BusinessException, DataIsEmptyException {
        if(insert(companyActivityApplyAddDTO.getCompanyActivityApply())){
            companyActivityApplyAddDTO.getCompanyActivityFile().forEach(d -> d.setCompanyActivityApplyId(companyActivityApplyAddDTO.getCompanyActivityApply().getId()));
            if(companyActivityFileService.insertBatch(companyActivityApplyAddDTO.getCompanyActivityFile())){

                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(companyActivityApplyAddDTO.getCompanyActivityApply().getId());
                applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.COMPANY_ACTIVITIES.getId());
                applyApprovalAddDTO.setApplyApprovalName(companyActivityApplyAddDTO.getCompanyActivityApply().getActivityName());
                applyApprovalAddDTO.setApprovalUserId(companyActivityApplyAddDTO.getCompanyActivityApply().getPorposer());

                return applyApprovalService.addApproval(applyApprovalAddDTO);
            }
        }
        throw new BusinessException("提交申请失败!");
    }
}
